import { degreesToRadians, wgs84ToCar } from '../script/utils/crs'
// 相机飞行定位
const flyTo = (viewer, position, completeFunc, cancelFunc) => {
  // 将三维球定位到玛纳斯
  viewer.camera.flyTo({
    destination: wgs84ToCar(position[0], position[1], position[2]),
    orientation: {
      heading: degreesToRadians(360),
      pitch: degreesToRadians(-90),
      roll: degreesToRadians(0)
    },
    // 定位完成之后的回调函数
    complete: completeFunc,
    // 取消定位之后的回调函数
    cancel: cancelFunc
  })
}

// 放大
const zoomIn = viewer => {
  // 获取当前镜头位置的笛卡尔坐标
  let cameraPos = viewer.scene.camera.position
  // 获取当前坐标系标准
  let ellipsoid = viewer.scene.globe.ellipsoid
  // 根据坐标系标准，将笛卡尔坐标转换为地理坐标
  let cartographic = ellipsoid.cartesianToCartographic(cameraPos)
  // 获取镜头的高度
  let height = cartographic.height
  // 放大
  if (height < 100) {
    return
  }
  viewer.scene.camera.zoomIn(height / 3)
}

// 缩小
const zoomOut = viewer => {
  // 获取当前镜头位置的笛卡尔坐标
  let cameraPos = viewer.scene.camera.position
  // 获取当前坐标系标准
  let ellipsoid = viewer.scene.globe.ellipsoid
  // 根据坐标系标准，将笛卡尔坐标转换为地理坐标
  let cartographic = ellipsoid.cartesianToCartographic(cameraPos)
  // 获取镜头的高度
  let height = cartographic.height
  // 缩小
  if (height > 146667046.90076026) {
    return
  }
  viewer.scene.camera.zoomOut(height * 1.2)
}

export { flyTo, zoomIn, zoomOut }
